package com.psdi.element;

import com.psdi.manager.ElementManager;
import com.psdi.manager.GameLoader;
import com.psdi.physics.Collider;
import com.psdi.window.GameJFrame;

import java.util.HashSet;
import java.util.Set;

/**
 * Bullet类代表游戏中的子弹元素。
 * 它可以包含子弹的属性和方法，例如位置、速度、伤害等。
 */
public class Bullet extends ElementObject implements Collider {
    private int speed; // 子弹速度
    private float atk;
    private  Vector2D bulletdestination=new Vector2D(1000,1000);//子弹移动目的地与自身的向量

    public Bullet() {
        transform = new Transform();
        tag=Tag.BULLET; // 设置标签为BULLET
    }


    @Override
    public void update() {
        //搜寻最近敌人
        Searchclosesttarget();
        // 更新子弹位置
        if (!isAlive()) return; // 如果子弹不存活，则不更新位置
        transform.position.add(bulletdestination.normalize().scalarMultiply(speed));
        transform.rotation.z+=30f;
        setImage(GameLoader.iconMap2.get(tag.toString()).get(0));
        if (transform.position.x<0 || transform.position.x > GameJFrame.GAME_WIDTH ||
            transform.position.y < 0 || transform.position.y > GameJFrame.GAME_HEIGHT) {
            setAlive(false); // 如果子弹超出边界，则标记为不存活
        }
    }
    private void Searchclosesttarget(){
        if(!ElementManager.getINSTANCE().getGameElements().get(Tag.ENEMY).isEmpty()&&ElementManager.getINSTANCE().getGameElements().get(Tag.BOSS).isEmpty()) {
            for (int i=ElementManager.getINSTANCE().getGameElements().get(Tag.ENEMY).size()-1;i>=0;i--){
                Enemy enemy = (Enemy) ElementManager.getINSTANCE().getGameElements().get(Tag.ENEMY).get(i);
                Vector2D dis = Vector2D.displacement(transform.position, enemy.transform.position);
                if(dis.module()<bulletdestination.module()){
                    bulletdestination = dis;
                }
            }
        }
        else if(!ElementManager.getINSTANCE().getGameElements().get(Tag.BOSS).isEmpty()){
            Boss boss = (Boss) ElementManager.getINSTANCE().getGameElements().get(Tag.BOSS).get(0);
            bulletdestination= Vector2D.displacement(transform.position, boss.transform.position);
        }
    }
    @Override
    public ElementObject create(String string) {
        String[] split = string.split(";");
        for (String s : split) {
            switch (s.split(":")[0].trim()) {
                case "x":
                    transform.position.x = Float.parseFloat(s.split(":")[1].trim());
                    break;
                case "y":
                    transform.position.y = Float.parseFloat(s.split(":")[1].trim());
                    break;
                case "atk":
                    atk = Float.parseFloat(s.split(":")[1].trim());
                    break;
                    //用bulletdestination来代替子弹移动方向
//                case "direction":
//                    direction = Vector2D.parseVector2D(s.split(":")[1].trim());
//                    break;
            }
        }
        tag=Tag.BULLET;
        setImage(GameLoader.iconMap2.get(tag.toString()).get(0));
        speed=6;
        transform.size = new Size(20, 20);
        return this;
    }

    @Override
    public Size colliderSize() {
        return transform.size;
    }

    @Override
    public Position colliderPosition() {
        return transform.position;
    }

    @Override
    public Tag colliderTag() {
        return tag;
    }

    @Override
    public Set<Tag> colliderFilterTag() {
        Set<Tag> tags = new HashSet<>();
        tags.add(Tag.PLAYER);
        tags.add(Tag.KNIFE);
        tags.add(Tag.MAP);
        tags.add(Tag.BOSSBULLET);
        return tags;
    }

    @Override
    public void onCollisionStay(ElementObject other) {
        if(other instanceof Enemy) {
            ((Enemy) other).setHp(((Enemy) other).getHp() - atk);
            other.transform.position.add(((Enemy) other).direction.scalarMultiply(-5));
            setAlive(false); // 子弹与其他元素碰撞时，子弹消失
        }
        else if(other instanceof Boss) {
            ((Boss) other).setHp(((Boss) other).getHp() - atk);
            setAlive(false); // 子弹与其他元素碰撞时，子弹消失
        }

    }

}
